import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { ArticleCategory } from '@/modules/article/article_category/entities/category.entity';
import { ArticleTag } from '@/modules/article/article_tag/entities/tag.entity';
import { User } from '@/modules/user/entities/user.entity';

export enum ArticleStatus {
  /**
   * 草稿
   */
  DRAFT = 'draft',
  /**
   * 待审核
   */
  PENDING = 'pending',
  /**
   * 已发布
   */
  PUBLISHED = 'published',
  /**
   * 锁定
   */
  LOCKED = 'locked',
}

@Entity()
export class Article {
  @PrimaryGeneratedColumn()
  article_id: number;

  @ManyToOne(() => User, (user) => user.id, { nullable: false })
  @JoinColumn({ name: 'author_id' })
  author: User;

  @Column({ type: 'varchar', length: 100, comment: '文章标题' })
  article_title: string;

  @Column({ type: 'varchar', length: 255, comment: '文章摘要' })
  article_description: string;

  // 默认不查询，需要时使用QueryBuilder的addSelect查询
  @Column({ type: 'longtext', comment: '文章内容', select: false })
  article_content: string;

  @Column({ type: 'varchar', length: 500, default: '', comment: '封面图' })
  cover_src: string;

  @Column({ type: 'bigint', default: 0, comment: '浏览量' })
  article_views: number;

  @Column({ type: 'bigint', default: 0, comment: '评论数' })
  article_comment_count: number;

  @Column({ type: 'bigint', default: 0, comment: '点赞数' })
  article_like_count: number;

  @Column({ type: 'enum', enum: ArticleStatus, default: ArticleStatus.DRAFT, comment: '文章状态' })
  article_status: ArticleStatus;

  @ManyToMany(() => ArticleCategory)
  @JoinTable({
    name: 'article_categories_relation',
    joinColumn: {
      name: 'article_id',
      referencedColumnName: 'article_id',
    },
    inverseJoinColumn: {
      name: 'category_id',
      referencedColumnName: 'category_id',
    },
  })
  categories: ArticleCategory[];

  @ManyToMany(() => ArticleTag)
  @JoinTable({
    name: 'article_tags_relation',
    joinColumn: {
      name: 'article_id',
      referencedColumnName: 'article_id',
    },
    inverseJoinColumn: {
      name: 'tag_id',
      referencedColumnName: 'tag_id',
    },
  })
  tags: ArticleTag[];

  @Column({ type: 'timestamp', comment: '发布日期' })
  article_public_date: Date;

  @CreateDateColumn({ type: 'timestamp', comment: '创建日期' })
  create_date: Date;

  @UpdateDateColumn({ type: 'timestamp', comment: '更新日期' })
  update_date: Date;

  @DeleteDateColumn({ type: 'timestamp', select: false, comment: '删除日期' })
  remove_date: Date;
}
